package puyu.manage.service.utils.except;

import org.apache.commons.beanutils.BeanUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class AppException extends GeneralException {
    private static final long serialVersionUID = -2122941609576536542L;

    public AppException() {
    }

    public AppException(String code, String message, String systemMessage) {
        super(code, message, systemMessage);
    }

    public AppException(String code) {
        super(code);
    }

    public AppException(String code, String message) {
        super(code, message, (String)null);
    }

    public AppException(String code, Exception ex, String errorType) {
        this(code);
        if (ex != null) {
            ExceptionFormatter formatter = new ExceptionFormatter(ex);
            this.setSystemMessage(formatter.getStackMessage());
            this.setMessage(ex.getMessage());
        }

    }

    public AppException(String code, HashMap paraInfo, String errorType) {
        this(code, paraInfo);
    }

    public AppException(String code, HashMap paraInfo) {
        this(code);
        if (paraInfo != null) {
            Set setInfo = paraInfo.keySet();
            Iterator iteInfo = setInfo.iterator();

            while(iteInfo.hasNext()) {
                String key = (String)iteInfo.next();
                String value = (String)paraInfo.get(key);
                this.setMessage(this.getMessageNoCode().replaceAll(key, value));
            }
        }

    }

    public AppException(String code, HashMap paraInfo, Exception ex, String errorType) {
        this(code, paraInfo, ex);
    }

    public AppException(String code, HashMap paraInfo, Exception ex) {
        this(code);
        if (paraInfo != null) {
            Set setInfo = paraInfo.keySet();
            Iterator iteInfo = setInfo.iterator();

            while(iteInfo.hasNext()) {
                String key = (String)iteInfo.next();
                String value = (String)paraInfo.get(key);
                this.setMessage(this.getMessageNoCode().replaceAll(key, value));
            }
        }

        if (ex != null) {
            ExceptionFormatter formatter = new ExceptionFormatter(ex);
            this.setSystemMessage(formatter.getStackMessage());
        }

    }

    public AppException(String code, String[] messages, String[] systemMessages) {
        this(code);
        int i;
        if (this.getMessageNoCode() != null) {
            for(i = 0; i < messages.length; ++i) {
                this.setMessage(this.getMessageNoCode().replaceAll("\\{" + i + "\\}", "[" + messages[i] + "]"));
            }
        }

        if (this.getSystemMessage() != null) {
            for(i = 0; i < systemMessages.length; ++i) {
                this.setSystemMessage(this.getSystemMessage().replaceAll("\\{" + i + "\\}", "[" + systemMessages[i] + "]"));
            }
        }

    }

    public AppException(String code, String message, String systemMessage, Exception ex) {
        super(code, message, systemMessage + ex.getMessage());
    }

    public AppException(Throwable targetEx) {
        super(targetEx);

        try {
            try {
                String code = BeanUtils.getProperty(targetEx, "code");
                this.setCode(code);
            } catch (Exception var5) {
            }

            try {
                String message = BeanUtils.getProperty(targetEx, "message");
                this.setMessage(message);
            } catch (Exception var4) {
            }

            this.setStackTrace(targetEx.getStackTrace());
        } catch (Exception var6) {
            var6.printStackTrace();
        }

    }
}
